﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.Admin.Account
{
    public partial class ModifyAcount : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bool parseSucceed;

                Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

                if (parseSucceed)
                {
                    BindData(id);
                }
                else
                {
                    throw new Exception("对不起，你所访问的页面不存在");
                }
            }
        }

        protected void BindData(Guid id)
        {
            Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(id);

            if (account == null)
            {
                throw new Exception("对不起，你所访问的页面不存在");
            }

            if (account.IsAdmin)
            {
                if (!base.Account.HavePermission(Permission.AdminManage))
                {
                    throw new Exception("你无权查看此页面");
                }
            }
            else
            {
                if (!base.Account.HavePermission(Permission.AccountManage))
                {
                    throw new Exception("你无权查看此页面");
                }
            }

            AccountEmail.Text = account.Email;
            AccountName.Text = account.Name;
            AccountRemark.Text = account.Remark;
            AccountTel.Text = account.Telephone;

            // Bind Role
            if (!account.IsAdmin)
            {
                AccountRoleName.Text = account.Role.Name;
                AccountRoleName.Visible = true;
                RoleList.Visible = false;
            }
            else
            {
                IList<Zyg.SSO.Core.Business.Role> roles = Zyg.SSO.Core.Business.Role.GetRoles();

                int i = 0;
                RoleList.Items.Clear();
                foreach (Zyg.SSO.Core.Business.Role role in roles)
                {
                    RoleList.Items.Add(new ListItem(role.Name, role.Id.ToString()));

                    if (role.Id.ToString().Equals(account.RoleID.ToString()))
                    {
                        RoleList.Items[i].Selected = true;
                    }

                    i++;
                }
            }

            if (!string.IsNullOrEmpty(account.PictureAddress))
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath(account.PictureAddress));

                if (fileInfo.Exists)
                {
                    AccountPic.ImageUrl = account.PictureAddress;
                }
                else
                {
                    AccountPic.ImageUrl = "~/Content/Account/no_head.gif";
                }
            }
            else
            {
                AccountPic.ImageUrl = "~/Content/Account/no_head.gif";
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            bool parseSucceed;

            Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

            Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(id);

            account.Name = AccountName.Text;
            account.Telephone = AccountTel.Text;
            account.Remark = AccountRemark.Text;

            if (account.IsAdmin)
            {
                account.RoleID = new Guid(RoleList.SelectedValue);
            }

            if (ModifyPicHidden.Value.Equals("true") && FileAccountPicutre.HasFile)
            {
                string accountContentPath = Server.MapPath("~/Content/Account/");
                string extension = Zyg.Utility.Helper.FileHelper.GetFileExtension(FileAccountPicutre.FileName);

                string fileName = account.Id.ToString() + extension;

                account.PictureAddress = "~/Content/Account/" + fileName;

                FileAccountPicutre.SaveAs(accountContentPath + fileName);
            }

            account.Save();

            Zyg.SSO.Core.Business.Log log = new Zyg.SSO.Core.Business.Log();
            log.AccountId = base.Account.Id;
            log.ModuleId = account.IsAdmin ? Zyg.SSO.Core.Business.Module.App.Id : Zyg.SSO.Core.Business.Module.Account.Id;
            log.Message = string.Format("管理员 {0} 修改了{1} {2}的信息", base.Account.Email, account.IsAdmin ? "管理员" : "用户", account.Email);

            log.Save();

            BindData(id);
        }
    }
}
